﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameLib.Serialization;

namespace Sandbox.Serialization.Serializers
{
	public class CommonValueSerializer : StringSerializer
	{
		protected override object FromString(Type type, string str, DataFlags dataFlags)
		{
			try
			{
				return Convert.ChangeType(str, type);
			}
			catch (InvalidCastException)
			{
				throw new Exception(string.Format("Default string converter cannot convert {0}, please add special string serializer", type));
			}
		}

		public override string ConvertToString(object obj, DataFlags dataFlag)
		{
			if (obj == null)
				return NullString;
			else
				return obj.ToString();
		}

		public override Type SupportedType
		{
			get { return typeof(object); }
		}
	}
}
